using System.Web.Mvc;
using ServiceDeclaration.ViewModels;
using WebLogic.Interface;
using WebLogic.Models;

namespace WebLogic.Controllers
{
    public class AccountController:Controller
    {
        private readonly IApplicationService applicationService;
        private readonly IMembershipService membershipService;
        private readonly IFormAuthenticationService formsService;
        public AccountController()
        {
            membershipService = new AccountMembershipService();
            formsService = new FormsAuthenticationService();
            applicationService = new ApplicationServiceClient();
        }

        
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(string username, string password)
        {
            if (membershipService.ValidateUser(username, password))
            {
                formsService.SignIn(username, true);
                return Redirect("/Home/Index");
            }
            else
            {
                return Redirect("/Account/FailedToLogin");
            }
        }

        public ActionResult FailedToLogin()
        {
            return View();
        }
        [Authorize]
        public ActionResult LogOff()
        {
            formsService.SignOut();
            return Redirect("/Home/Index");
        }
        [Authorize]
        public ActionResult AllUsers()
        {
            return View(applicationService.GetAllUsers());
        }
        [Authorize]
        [HttpPost]
        public ActionResult AddUser(string name, string password)
        {
            membershipService.CreateUser(name, password);
            return Redirect("/Account/AllUsers");
        }
        [Authorize]
        [HttpPost]
        public void DeleteUser(int userId)
        {
            applicationService.DeleteUser(new UserViewModel(){Id = userId});
        }
    }

}